﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using KPIS.GERP.GHRMS.BLL;
using KPIS.GERP.GHRMS.APP.Classes;
using KPIS.GERP.GHRMS.MODEL;
using KPIS.GERP.GHRMS.IBLL;

namespace KPIS.GERP.GHRMS.APP
{
    public partial class PS_NonWorkingJobSites : System.Web.UI.Page
    {
        private INonWorkingJobSiteBLL njsBLL = new NonWorkingJobSiteBLL();
        private INonWorkingDayBLL nwdBLL = new NonWorkingDayBLL();
        private ITreeViewBLL treeBLL = new TreeViewBLL();

        private NonWorkingDayInfo nwdInfo;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                string NwdSeq = Request.QueryString["non_working_seq"].ToString();
                lblNwdSeq.Text = NwdSeq;
                IList<NonWorkingDayInfo> nwdList = nwdBLL.ListNonWorkingDayBySequence(Int32.Parse(NwdSeq));
                nwdInfo = nwdList[0];
                lblNwdDate.Text = nwdInfo.Name + " ที่ " + string.Format("{0:dd/MMMM/yyyy}", nwdInfo.Date);

                LoadRootNodes(RadTreeView1, TreeNodeExpandMode.ServerSideCallBack);
                //RadTreeNode selected_node = RadTreeView1.SelectedNode;

                LoadDataGrid();
            }
        }
        public void LoadTreeView()
        {
            IList<TreeViewInfo> infoList = treeBLL.LoadJobSiteTreeViewForNWD("", Int32.Parse(lblNwdSeq.Text));

            RadTreeView1.DataSource = infoList;
            RadTreeView1.DataBind();
        }
        public void LoadDataGrid()
        {
            int NwdSeq = Convert.ToInt32(Request.QueryString["non_working_seq"].ToString());

            IList<NonWorkingJobSiteInfo> Info = njsBLL.ListNonWorkingJobSites(NwdSeq);
            RadGrid1.DataSource = Info;
            RadGrid1.DataBind();
        }
        private void LoadRootNodes(RadTreeView treeView, TreeNodeExpandMode expandMode)
        {
            IList<TreeViewInfo> infoList = treeBLL.LoadJobSiteTreeViewForNWD("", Int32.Parse(lblNwdSeq.Text));

            treeView.Nodes.Clear();

            foreach (TreeViewInfo info in treeBLL.LoadJobSiteTreeViewForNWD("", Int32.Parse(lblNwdSeq.Text)))
            {
                RadTreeNode node = new RadTreeNode();
                node.Text = info.Name;
                node.ToolTip = info.Descr;
                node.Value = info.Id;
                node.ImageUrl = info.Image_url;
                if (Convert.ToInt32(info.Children_count) > 0)
                {
                    node.ExpandMode = expandMode;
                }
                treeView.Nodes.Add(node);
            }
        }
        private void PopulateNodeOnDemand(RadTreeNodeEventArgs e, TreeNodeExpandMode expandMode)
        {
            e.Node.Dispose();
            e.Node.Nodes.Clear();

            foreach (TreeViewInfo info in treeBLL.LoadJobSiteTreeViewForNWD(e.Node.Value, Int32.Parse(lblNwdSeq.Text)))
            {
                RadTreeNode node = new RadTreeNode();
                node.Text = info.Name;
                node.ToolTip = info.Descr;
                node.Value = info.Id;
                node.ImageUrl = info.Image_url;
                if (Convert.ToInt32(info.Children_count) > 0)
                {
                    node.ExpandMode = expandMode;
                }
                e.Node.Nodes.Add(node);
            }

            e.Node.Expanded = true;
            e.Node.ExpandMode = expandMode;
        }

        public void PopulateNodeOnEditGrid(RadTreeNodeEventArgs e, TreeNodeExpandMode expandMode)
        {
            foreach (TreeViewInfo info in treeBLL.LoadJobSiteTreeViewForNWD(e.Node.Value, Int32.Parse(lblNwdSeq.Text)))
            {
                RadTreeNode node = new RadTreeNode();
                node.Text = info.Name;
                node.ToolTip = info.Descr;
                node.Value = info.Id;
                node.ImageUrl = info.Image_url;
                if (Convert.ToInt32(info.Children_count) > 0)
                {
                    node.ExpandMode = expandMode;
                }
                e.Node.Nodes.Add(node);
            }

            e.Node.Nodes.Clear();
            e.Node.ExpandMode = expandMode;
        }
        private void AddCheckedNodes(RadTreeView treeView)
        {
            nwdInfo = new NonWorkingDayInfo();
            IList<RadTreeNode> nodeCollection = treeView.CheckedNodes;

            foreach (RadTreeNode node in nodeCollection)
            {
                string value = node.Value;
                string[] root_value = node.Value.Split(new string[] { "-" }, StringSplitOptions.None);

                if (value.Contains("-"))
                {
                    njsBLL.Insert(Int32.Parse(Request.QueryString["non_working_seq"].ToString())
                        , root_value[1].ToString()
                        , ""
                        , 1);                  
                }
                
            }
            LoadRootNodes(RadTreeView1, TreeNodeExpandMode.ServerSideCallBack);
        }

        protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e)
        {
            if (e.CommandName == "Delete")
            {
                string Seq = (e.Item as GridDataItem).OwnerTableView.DataKeyValues[e.Item.ItemIndex]["Sequence"].ToString();

                nwdBLL.Delete(Int32.Parse(Seq), 1);
            }
            LoadRootNodes(RadTreeView1, TreeNodeExpandMode.ServerSideCallBack);
            LoadDataGrid();
        }

        protected void cmdAddItem_Click(object sender, EventArgs e)
        {
            AddCheckedNodes(RadTreeView1);
            LoadRootNodes(RadTreeView1, TreeNodeExpandMode.ServerSideCallBack);
            LoadDataGrid();
        }

        protected void RadTreeView1_NodeClick(object sender, RadTreeNodeEventArgs e)
        {

        }

        protected void Button2_Click(object sender, EventArgs e)
        {

        }

        protected void RadTreeView1_NodeExpand(object sender, RadTreeNodeEventArgs e)
        {
            PopulateNodeOnDemand(e, TreeNodeExpandMode.ServerSideCallBack);
        }

    }
}
